﻿// This source file is part of WallpaperControl, a tool for automatic changing 
// of the desktop wallpaper.
// 
// Visit http://wpcontrol.sourceforge.net/ for more.
//
// Copyright (c) 2014 nscrolldev
//
// Licensed under the GPLv2.
// See http://www.gnu.org/licenses/gpl-2.0.html for details.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WallpaperControl
{
    /// <summary>
    /// Condition that changes the wallpaper after a given time.
    /// </summary>
    internal class CondTimeInterval : Condition
    {
        /// <summary>
        /// Constructor. Creates a new condition object of type time interval.
        /// </summary>
        /// <param name="intervalSeconds">The interval after which the wallpaper 
        /// should be changed in seconds.</param>
        public CondTimeInterval(int intervalSeconds)
        {
            this.Interval = intervalSeconds;

            Program.ControlPanel.radioButtonInterval.Checked = true;
            Program.ControlPanel.comboBoxInterval.SelectedIndex = Array.IndexOf(IntervalSeconds, this.Interval);
        }

        /// <summary>
        /// The ID of this condition.
        /// </summary>
        internal const int CondId = 2;

        /// <summary>
        /// The timer that changes the wallpaper when the specified time is gone.
        /// </summary>
        private Timer _intervalTimer;

        /// <summary>
        /// The interval in which the wallpaper should be changed.
        /// </summary>
        private int _interval;

        /// <summary>
        /// The seconds that can be chosen as time interval.
        /// </summary>
        internal static int[] IntervalSeconds = new int[] 
            {
                // 30 seconds
                30,
                // 1 minute
                60,
                // 3 minutes
                180,
                // 5 minutes
                300,
                // 10 minutes
                600,
                // 15 minutes
                900,
                // 30 minutes
                1800,
                // 45 minutes
                2700,
                // 1 hour
                3600,
                // 2 hours
                7200,
                // 3 hours
                10800,
                // 4 hours
                14400,
                // 5 hours
                18000,
                // 6 hours
                21600,
                // 7 hours
                25200,
                // 8 hours
                28800,
                // 9 hours
                32400,
                // 10 hours
                36000,
                // 11 hours
                39600,
                // 12 hours
                43200,
                // TEST: 5 seonds
                5
            };

        internal override int Id
        {
            get 
            {
                return CondTimeInterval.CondId;
            }
        }

        /// <summary>
        /// Gets or sets the interval in which the wallpaper should be changed.
        /// </summary>
        /// <value>The interval in which the wallpaper should be changed.</value>
        public int Interval
        {
            get
            {
                return this._interval;
            }
            set
            {
                if (value > 0)
                {
                    this._interval = value;
                }
                else
                {
                    throw new NotSupportedException();
                }
            }
        }

        /// <summary>
        /// Gets or sets the timer repsonsible for changing the wallpaper.
        /// </summary>
        /// <value>The change timer.</value>
        public Timer Timer
        {
            get
            {
                return this._intervalTimer;
            }
            set
            {
                this._intervalTimer = value;
            }
        }

        /// <summary>
        /// Gets the time until the next wallpaper change.
        /// </summary>
        internal override TimeSpan RemainingTime
        {
            get
            {
                int completeInterval = this.Interval;
                // TODO: Timer.Interval is always the start interval
                int currentInterval = this.Timer.Interval / 1000;
                TimeSpan result = new TimeSpan(0, 0, 0, completeInterval - currentInterval, 0);
                return result;
            }
        }

        /// <summary>
        /// Gets or sets the value for this condition.
        /// </summary>
        internal override object Value
        {
            get
            {
                return this.Interval;
            }
            set
            {
                this.Interval = Convert.ToInt32(value);
            }
        }

        /// <summary>
        /// Checks if the current condition is true and executes the wallpaper 
        /// change.
        /// </summary>
        internal override void Check()
        {
            // Create timer
            this.Timer = new Timer();

            if (Program.CurrentSession.IsActive)
            {
                // Set first wallpaper
                //WallpaperManager.Instance.SetWallpaper(0);
                // Change wallpaper after selected interval is over
                this.Timer.Interval = this.Interval * 1000;
                this.Timer.Tick += new EventHandler(this.IntervalTimer_Event);
                this.Timer.Start();
            }
        }

        /// <summary>
        /// The action that is executed when the timer is finished.
        /// </summary>
        private void IntervalTimer_Event(object sender, EventArgs e)
        {
            WallpaperManager.Instance.SetNextWallpaper();
        }

        /// <summary>
        /// Action that is executed when the user changes the wallpaper manually in 
        /// a running session.
        /// </summary>
        internal override void ManualChangeCallback()
        {
            // Restart the timer
            this.Timer.Stop();
            this.Timer.Start();
        }

        /// <summary>
        /// Stops the timer.
        /// </summary>
        internal override void StopSession()
        {
            this.Timer.Stop();
        }

        /// <summary>
        /// Reads the interval from the combobox and applies it.
        /// </summary>
        internal override void ApplyValue()
        {
            this.Interval = IntervalSeconds[Program.ControlPanel.comboBoxInterval.SelectedIndex];
        }
    }
}
